<?php
namespace    Wap\Service;

use Wap\Model;

class RuleService
{
    private static $config = array( //默认的设置
        "time" => array("begin_time" => "", "end_time" => "0"),
        "sum_num" => 12,//每天点赞的总数
        'per_num_sum' => 999,
        "per_num" => 1,//对单个人的点赞数量限制
        'limit_sum' => 999,
        "ip" => ""//IP限制
    );


    public static function is_good($praise, $openid, $datas, $config = array())//$datas =	array( "zid" => "", "time" => "", "ip" => "" );
    {
        $rule = array();
        $result = array("error" => 0);

        $config = D("praise")
            ->field("per_num, per_num_sum, sum_num, limit_sum, startdate, enddate")
            ->where("id = %d", array($praise))
            ->cache("wap_praise_is_good" . $praise, 60)
            ->find();//查看活动设置的规则
        if (count($config) <= 0) {
            return 400;//活动不存在
        }
        if ($config['startdate'] > $datas['updated_time']) {
            return 401;//活动还没开始
        }
        if ($config['enddate'] < $datas['updated_time']) {
            return 402;//活动已经结束
        }

        if (count($config) > 0) {//自定义限制
            $tmp = array_intersect_key(self::$config, $config);
            foreach ($tmp as $key => $value) {
                $rule[$key] = $config[$key];
            }
        } else {
            $rule = self::$config;//默认限制
        }

        if (count($rule) <= 0) {
            $result['error'] = 1;
            $result['msg'] = "no rule found";

            return $result;
        }
        if (isset($rule['per_num_sum']) && $rule['per_num_sum'] > -1) {
            $pid = $datas['pid'];
            $config = array('per_num_sum' => $rule['per_num_sum']);
            $result['per_num_sum'] = self::is_good_per_num_sum($datas['praise'], $openid, $pid, $config);
        }
        if (isset($rule['per_num_sum']) && $rule['per_num_sum'] > -1 && $result['per_num_sum']['error']) {
            $result['error'] = 1;
            return $result;
        }

        if (isset($rule['limit_sum']) && $rule['limit_sum'] > -1) {//投票机会限制
            $config = array('limit_sum' => $rule['limit_sum']);
            $result['limit_sum'] = self::is_good_limit($datas['praise'], $openid, $datas['praise'], $config);
        }
        if (isset($rule['limit_sum']) && $rule['limit_sum'] > -1 && $result['limit_sum']['error']) {
            $result['error'] = 1;
            return $result;
        }

        if (isset($rule['sum_num']) && $rule['sum_num'] > -1) {//每天最多给多少选手投票
            $config = array("sum_num" => $rule['sum_num']);
            $result['sum_num'] = self::is_good_sum_num($openid, $datas['praise'], $config);
        }
        if (isset($rule) && $rule['sum_num'] > -1 && $result['sum_num']['error']) {
            $result['error'] = 1;
            return $result;
        }

        if (isset($rule['per_num']) && $rule['per_num'] > -1) {//每天最多给一个选手投多少票
            $pid = $datas['pid'];
            $config = array("per_num" => $rule['per_num']);
            $result['per_num'] = self::is_good_per_num($datas['praise'], $openid, $pid, $config);
        }
        if (isset($rule['per_num']) && $rule['per_num'] > -1 && $result['per_num']['error']) {
            $result['error'] = 1;
            return $result;
        }
        if ($rule['per_num'] == -1) {
            $result['per_num']['msg'] = "恭喜您为该项添人气成功！";
        }
        if ($rule['sum_num'] == -1) {
            $result['sum_num']['msg'] = "";
        }
        return $result;
    }

    public static function is_good_sum_num($openid, $praise, $config)//每天点赞总数量限制
    {
        $t = time();
        $begin_time = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));
        $end_time = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));

        $mode = D("praiseItemRecord");
        $count = $mode->fetch_openid_votes($openid, $praise, $begin_time, $end_time);

        $result = array("error" => 0, "msg" => "", 'left' => 0);
        if ($count >= $config['sum_num']) {
            $result['error'] = 1;
            $result['msg'] = "今天机会已经用完";
        } else {
            //$result['left']	=	$config['sum_num'] - $count - 1;
            $result['left'] = $config['sum_num'] - $count;
            if ($praise == 19) {
                $result['msg'] = "除此之外，您今天还可以为另外" . ($result['left'] - 1) . "家企业增加人气值";
            } else if ($praise == 20) {
                $result['msg'] = "除此之外，您今天还可为另外" . ($result['left'] - 1) . "位候选人增加人气值";
            } else {
                $result['msg'] = "";//"今天还可以为".($result['left'] - 1)."幅作品增加人气值";
            }
        }
        if($count > 0){
            $result['ex'] = 1;
        }else{
            $result['ex'] = 0;
        }

        return $result;
    }

    public static function is_good_per_num($praise, $openid, $pid, $config)//对单个人的点赞总数量限制
    {
        $begin_time = 0;
        $end_time = 0;
        //if( $scope === 1 ){
        $t = time();
        $begin_time = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));
        $end_time = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));
        //}

        $mode = D("praiseItemRecord");
        $count = $mode->fetch_pid_votes($praise, $openid, $pid, $begin_time, $end_time);

        $result = array("error" => 0, "msg" => "", "left" => 0);
        if ($count >= $config['per_num']) {
            $result['error'] = 1;
            // if( $scope === 1 ){
            if ($praise == 19) {
                $result['msg'] = "已经为该企业添过人气";
            } else if ($praise == 20) {
                $result['msg'] = "已经为该候选人添过人气";
            } else {
                $result['msg'] = "今天给该作品的投票次数已经用完~~~";
            }
            // }else if( $scope === 0 ){
            // $result['msg'] = '给该作品的投票次数已经用完';
            // }
        } else {
            // if( $scope === 1 ){
            $result['left'] = $config['per_num'] - $count;
            // }else if( $scope === 0 ){
            // $result['left'] = $config['per_num_sum'] - $count;
            // }
            if ($praise == 19) {
                // $result['msg'] = "还可以为该企业增加".($result['left'] - 1)."个人气值";
                $result['msg'] = "";
            } else if ($praise == 20) {
                $result['msg'] = "";
            } else {
                $result['msg'] = "恭喜您为该项添人气成功！";//"还可以为该作品增加".($result['left'] - 1)."个人气值";
            }
        }

        return $result;
    }

    public static function is_good_per_num_sum($praise, $openid, $pid, $config)
    {
        $mode = D("praiseItemRecord");
        $count = $mode->fetch_pid_votes($praise, $openid, $pid);
        $result = array('error' => 0, 'msg' => '', 'left' => 0);
        if ($count >= $config['per_num_sum']) {
            $result['error'] = 1;
            if ($praise == 19) {
                $result['msg'] = "已经为该企业添过人气";
            } else if ($praise == 20) {
                $result['msg'] = "已经为该候选人添过人气";
            } else {
                $result['msg'] = "您已为该项添加" . $config['per_num_sum'] . "票,票数已经用完";
            }
        } else {
            $result['left'] = $config['per_num_sum'] - $count;
            $result['msg'] = "恭喜您为该项添人气成功！！";//"还可以为该作品增加".($result['left'] - 1)."个人气值";
        }
        return $result;
    }

    public static function is_good_limit($praise, $openid, $pid, $config)
    {
        $mode = D('praiseItemRecord');
        $count = $mode->fetch_praise_votes($praise, $openid);
        $result = array('error' => 0, 'msg' => '', 'left' => 0);
        if ($count >= $config['limit_sum']) {
            $result['error'] = 1;
            $result['msg'] = '投票机会已经用完';
        } else {
            $result['left'] = $config['limit_sum'] - $count;
            // $result['msg'] = "还有".($result['left'] - 1 ).'投票机会';
            if ($praise == 19) {
                $result['msg'] = "还可以为该企业增加" . ($result['left'] - 1) . "个人气值";
            } else if ($praise == 20) {
                $result['msg'] = "还可以为该候选人增加" . ($result['left'] - 1) . "个人气值";
            } else {
                $result['msg'] = "恭喜您为该项添人气成功！！！";//"还可以为该作品增加".($result['left'] - 1)."个人气值";
            }
        }

        return $result;
    }
}

?>
